{% extends 'base.html' %}

{% block content %}
<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1>音乐列表</h1>
        {% if current_user.is_authenticated %}
        <a href="{{ url_for('add_music') }}" class="btn btn-primary">添加音乐</a>
        {% endif %}
    </div>

    <div class="row">
        <div class="col-md-3">
            <div class="list-group">
                <a href="{{ url_for('music') }}" 
                   class="list-group-item list-group-item-action {% if not request.args.get('genre') %}active{% endif %}">
                    全部
                </a>
                <a href="{{ url_for('music', genre='流行') }}" 
                   class="list-group-item list-group-item-action {% if request.args.get('genre') == '流行' %}active{% endif %}">
                    流行
                </a>
                <a href="{{ url_for('music', genre='摇滚') }}" 
                   class="list-group-item list-group-item-action {% if request.args.get('genre') == '摇滚' %}active{% endif %}">
                    摇滚
                </a>
                <a href="{{ url_for('music', genre='民谣') }}" 
                   class="list-group-item list-group-item-action {% if request.args.get('genre') == '民谣' %}active{% endif %}">
                    民谣
                </a>
            </div>
        </div>

        <div class="col-md-9">
            <div class="row">
                {% for music in musics.items %}
                <div class="col-md-4 mb-4">
                    <div class="card">
                        {% if music.cover_url %}
                        <img src="{{ music.cover_url }}" class="card-img-top" alt="{{ music.title }}">
                        {% endif %}
                        <div class="card-body">
                            <h5 class="card-title">{{ music.title }}</h5>
                            <p class="card-text">
                                <small class="text-muted">
                                    {{ music.artist }}
                                    {% if music.genre %}
                                    <span class="badge badge-secondary">{{ music.genre }}</span>
                                    {% endif %}
                                </small>
                            </p>
                            <audio controls class="w-100">
                                <source src="{{ music.url }}" type="audio/mpeg">
                                Your browser does not support the audio element.
                            </audio>
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>

            {% if musics.pages > 1 %}
            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center">
                    {% if musics.has_prev %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('music', page=musics.prev_num, genre=request.args.get('genre')) }}">
                            上一页
                        </a>
                    </li>
                    {% endif %}
                    
                    {% for page in musics.iter_pages() %}
                        {% if page %}
                            <li class="page-item {% if page == musics.page %}active{% endif %}">
                                <a class="page-link" href="{{ url_for('music', page=page, genre=request.args.get('genre')) }}">
                                    {{ page }}
                                </a>
                            </li>
                        {% else %}
                            <li class="page-item disabled"><span class="page-link">...</span></li>
                        {% endif %}
                    {% endfor %}
                    
                    {% if musics.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('music', page=musics.next_num, genre=request.args.get('genre')) }}">
                            下一页
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %} 